import DB from '@/utils/sqlite.js';

/**
 * 获取当前车辆
 */
export const getCurrentCar = async () => {
  const id = uni.getStorageSync('currentCarId');
  if (id) {
    return await DB.getOneById('car', id);
  } else {
    const car = await DB.getList('car', {}, {}, 'id ASC');
    if (car.length > 0) {
      const first = car[0];
      uni.setStorageSync('currentCarId', first.id);
      return first;
    } else {
      return null;
    }
  }
};

/**
 * 设置当前车辆
 */
export const setCurrentCar = (carId) => {
  uni.setStorageSync('currentCarId', carId);
  return DB.getOneById('car', carId).then(car => {
    return !!car;
  });
};

/**
 * 获取车辆列表
 */
export const getCarList = () => {
  return DB.getList('car', {}, {}, 'id ASC');
};